package com.lianboke.link.ad.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lianboke.link.ad.R;
import com.lianboke.link.ad.activity.OrderTypeDetailsForHuZhuActivity;
import com.lianboke.link.ad.activity.OrderTypeDetailsForShiWanActivity;
import com.lianboke.link.ad.activity.OrderTypeDetailsForZhuCeActivity;
import com.lianboke.link.ad.activity.OrderTypeDetailsForZhuangJiActivity;
import com.lianboke.link.ad.beans.OrderListNewModel;
import com.lianboke.link.ad.entry.OrdetTypeTable;
import com.lianboke.link.ad.utils.OrderStatusUtils;
import com.lianboke.link.ad.utils.imageloader.GlideLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 类型任务数据适配器
 * Created by admin on 2018/1/5.
 */

public class OrdersUnfinishAdapter extends BaseAdapter {

    private Context context;
    private HashMap<Integer,Boolean> itempSelect = new HashMap<Integer,Boolean>();

    private List<OrdetTypeTable> datas = new ArrayList<OrdetTypeTable>();


    public OrdersUnfinishAdapter(Context con){
        context = con;
    }

    /**
     * s
     * @param data
     */
    public void setOrderDatas(List<OrdetTypeTable> data){
        if(datas!=null){
            datas.clear();
        }
        datas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(datas==null){
            return 0;
        }
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView =  LayoutInflater.from(context).inflate(R.layout.item_orders_unfinish_layout,null);
            viewHolder.setmIVOrderIcon((ImageView)convertView.findViewById(R.id.iv_order_icon));
            viewHolder.setmTextOrderTitle((TextView) convertView.findViewById(R.id.tv_select_lable_title));
            viewHolder.setmTextOrderDurDate((TextView) convertView.findViewById(R.id.tv_select_lable_durdate));
            viewHolder.setmTextOrderLastDate((TextView) convertView.findViewById(R.id.tv_select_lable_lastdate));
            viewHolder.setmIVOrderMark((ImageView) convertView.findViewById(R.id.iv_order_mark));
            viewHolder.setLl_select_lable_content((LinearLayout) convertView.findViewById(R.id.ll_select_lable_content));
            convertView.setTag(viewHolder);
        }else{
            viewHolder =(ViewHolder) convertView.getTag();
        }
        if(itempSelect.get(position)==null){
            itempSelect.put(position,false);
        }
        OrdetTypeTable ordetTypeTable = (OrdetTypeTable)getItem(position);
        if(TextUtils.isEmpty(ordetTypeTable.getImg())){
            viewHolder.getmIVOrderIcon().setBackgroundResource(R.drawable.icon_default);
        }else{
            GlideLoader.loadImage(ordetTypeTable.getImg(),viewHolder.getmIVOrderIcon());
        }
            String strDateSize = String.valueOf(OrderStatusUtils.getOrdersTimeSize(ordetTypeTable.getDurationLaunchTimes()));
        viewHolder.getmTextOrderTitle().setText(String.format(context.getResources().getString(R.string.order_finish_des),strDateSize));
        viewHolder.getmTextOrderDurDate().setText("任务需激活日期: \n"+ordetTypeTable.getDurationLaunchTimes());
        viewHolder.getmTextOrderLastDate().setText("最近激活日期: "+ordetTypeTable.getLastLaunchTime());

        //view 信息展开
        viewHolder.getmIVOrderMark().setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(itempSelect.get(position)){
                    itempSelect.put(position,false);
                    viewHolder.getmIVOrderMark().setImageResource(R.drawable.icon_down_gb);
                }else{
                    itempSelect.put(position,true);
                    viewHolder.getmIVOrderMark().setImageResource(R.drawable.iocn_up_bg);
                }
                viewHolder.getmTextOrderDurDate().setVisibility(View.VISIBLE);
                viewHolder.getmTextOrderLastDate().setVisibility(View.VISIBLE);
                notifyDataSetChanged();
            }
        });
        if(itempSelect.get(position)){
            viewHolder.getmIVOrderMark().setImageResource(R.drawable.iocn_up_bg);
            viewHolder.getmTextOrderDurDate().setVisibility(View.VISIBLE);
            viewHolder.getmTextOrderLastDate().setVisibility(View.VISIBLE);
        }else{
            viewHolder.getmIVOrderMark().setImageResource(R.drawable.icon_down_gb);
            viewHolder.getmTextOrderDurDate().setVisibility(View.GONE);
            viewHolder.getmTextOrderLastDate().setVisibility(View.GONE);
        }
        return convertView;
    }


    /**
     *view 对象
     */
    public class ViewHolder{
        private ImageView mIVOrderIcon;
        private TextView mTextOrderTitle;
        private TextView mTextOrderDurDate;
        private TextView mTextOrderLastDate;
        private ImageView mIVOrderMark;
        private LinearLayout ll_select_lable_content;

        public ViewHolder() {
        }

        public ImageView getmIVOrderIcon() {
            return mIVOrderIcon;
        }

        public void setmIVOrderIcon(ImageView mIVOrderIcon) {
            this.mIVOrderIcon = mIVOrderIcon;
        }

        public TextView getmTextOrderTitle() {
            return mTextOrderTitle;
        }

        public void setmTextOrderTitle(TextView mTextOrderTitle) {
            this.mTextOrderTitle = mTextOrderTitle;
        }

        public TextView getmTextOrderDurDate() {
            return mTextOrderDurDate;
        }

        public void setmTextOrderDurDate(TextView mTextOrderDurDate) {
            this.mTextOrderDurDate = mTextOrderDurDate;
        }

        public TextView getmTextOrderLastDate() {
            return mTextOrderLastDate;
        }

        public void setmTextOrderLastDate(TextView mTextOrderLastDate) {
            this.mTextOrderLastDate = mTextOrderLastDate;
        }

        public ImageView getmIVOrderMark() {
            return mIVOrderMark;
        }

        public void setmIVOrderMark(ImageView mIVOrderMark) {
            this.mIVOrderMark = mIVOrderMark;
        }

        public LinearLayout getLl_select_lable_content() {
            return ll_select_lable_content;
        }

        public void setLl_select_lable_content(LinearLayout ll_select_lable_content) {
            this.ll_select_lable_content = ll_select_lable_content;
        }
    }
}
